package com.project.service.base.ehcahe;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

@Service
public class EhcacheServiceImpl implements EhcacheService {

    private Cache captchaCache;

    @Autowired
    public EhcacheServiceImpl(CacheManager cacheManager) {
        this.captchaCache = cacheManager.getCache("captcha-cache");
    }

    //==============验证码============
    @Override
    //添加验证码到cache
    public void addCaptcha(String key, String captcha)
    {
        captchaCache.put(key, captcha);
    }

    @Override
    //通过手机号从cache获取验证码
    public String getCaptchaValueByMobile(String mobile){

        if(captchaCache.get(mobile)!=null)
            return (String)captchaCache.get(mobile).get();
        else
            return "";
    }

    @Override
    public String getCaptchaValueByEmail(String email) {
        if(captchaCache.get(email)!=null)
            return (String)captchaCache.get(email).get();
        else
            return "";
    }

    @Override
    public String getCaptchaValueByUsername(String username){
        if(captchaCache.get(username)!=null)
            return (String)captchaCache.get(username).get();
        else
            return "";
    }

    @Override
    //删除 验证码
    public void removeCaptcha(String key){
        captchaCache.evict(key);
    }

}
